package com.example.myapplication.ui.upload

import androidx.lifecycle.LiveData
import androidx.room.*
import com.example.myapplication.ui.FileBean
import com.example.myapplication.ui.download.FileDownloadBean
import kotlinx.coroutines.flow.Flow

@Dao
interface FileUploadDao {
    /**
     * 查询所有的数据，根据名字排序
     */
    @Query("SELECT * FROM file_upload_table ORDER BY name ASC")
    fun getAlphabetizedWords(): List<FileUploadBean>
    /**
     * 根据指定的UUID只获取一个数据
     */
    @Query("SELECT * FROM file_upload_table WHERE uuid=:uuid limit 1")
    fun getFileUploadBeanByUUID(uuid: String): FileUploadBean?
    /**
     * 根据UUID获取指定的数据，转为Livedata
     */
    @Query("SELECT * FROM file_upload_table WHERE uuid=:uuid limit 1")
    fun getFileUploadBeanByUUIDToLiveData(uuid: String): LiveData<FileUploadBean>
    /**
     * 根据 更新状态 类型排序 查询所有的数据 转为Flow
     */
    @Query("SELECT * FROM file_upload_table ORDER BY uploadState,type ASC")
    fun getAlphabetizedWordsByFlow(): Flow<List<FileUploadBean>>
    /**
     * 根据 更新状态 类型排序 查询所有的数据 转为LiveData
     */
    @Query("SELECT * FROM file_upload_table ORDER BY uploadState,type ASC")
    fun getAlphabetizedWordsByLiveData(): LiveData<List<FileUploadBean>>
    /**
     * 获取一个准备完成的任务
     */
    @Query("SELECT * FROM file_upload_table WHERE enabled = 1 and uploadState = 3  ORDER BY createAt  limit 1")
    fun getOtherNotUploadTask(): FileUploadBean?

    /**
     * 插入一条数据
     */
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(word: FileUploadBean)

    /**
     * 更新数据字段 上传ID，根据UUID
     */
    @Query("UPDATE OR ABORT file_upload_table SET uploadId = :uploadId , updateAt =:updateAt  WHERE `uuid` = :uuid ")
    fun update(uuid: String, uploadId: String, updateAt: Long = System.currentTimeMillis())
    /**
     * 更新数据字段，根据UUID
     */
    @Query("UPDATE OR ABORT file_upload_table SET uploadSize =:uploadSize , uploadState = :uploadState, updateAt =:updateAt , state =:state , uploadSpeed =:uploadSpeed WHERE `uuid` = :uuid ")
    fun update(
        uuid: String,
        uploadSize: Long,
        uploadState: Byte,
        uploadSpeed:Long ,
        state: String,
        updateAt: Long = System.currentTimeMillis()
    )
    /**
     * 更新所有的未完成的任务的显示状态
     */
    @Query("UPDATE OR ABORT file_upload_table SET   updateAt =:updateAt , state =:state , uploadState = 2  WHERE `length` <> `uploadSize` and enabled = 1 and  uploadState <> 3 ")
    fun updateAllTask(
        state: String = "${FileBean.STATE_NET_FLAG}网络异常，请点击重新下载",
        updateAt: Long = System.currentTimeMillis()
    )
    /**
     * 通过Card对指定的表更新
     */
    @Update(entity = FileUploadBean::class)
    fun updateByFileUploadCard(card: FileUploadCard): Int
    /**
     * 更新所有的失败的任务
     */
    @Query("UPDATE OR ABORT file_upload_table SET uploadState = 3 , updateAt =:updateAt , state = 'DEFAULT_正在等待...' WHERE uploadState = 2 ")
    fun updateErrorAll(updateAt: Long = System.currentTimeMillis()): Int

    /**
     * 删除所有任务
     */
    @Query("DELETE FROM file_upload_table")
    fun deleteAll(): Int
    /**
     * 删除所有完成的任务
     */
    @Query("DELETE FROM file_upload_table where enabled = 1 and (`uploadState` = 4 or `uploadSize` = `length`)")
    fun deleteFinishAll(): Int
    /**
     * 通过ID山删除任务
     */
    @Query("DELETE FROM file_upload_table Where `id` = :id")
    fun deleteById(id: Int): Int
    /**
     * 通过UUID删除指定任务
     */
    @Query("DELETE FROM file_upload_table Where `uuid` = :id")
    fun deleteByUUID(id: String): Int
    /**
     * 删除所有运行任务
     */
    @Query("DELETE FROM file_upload_table where enabled = 1 and `uploadState` = 1")
    fun deleteRunningAll(): Int
    /**
     * 删除所有异常任务
     */
    @Query("DELETE FROM file_upload_table where enabled = 1 and (`uploadState` = 2 or `uploadSize` <> `length`)")
    fun deleteErrorAll(): Int

}